package fy.form.oa.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.logging.LogFactory;

import fy.config.FyConfig;
import fy.util.DBUtil;

public class JDSerialDao {

    private static org.apache.commons.logging.Log log = LogFactory
            .getLog(JDSerialDao.class);

    private long summaryID;

    private String accoutID;

    public void setAccoutID(String accoutID) {
        this.accoutID = accoutID;
    }

    public void setColSummary(long summaryID) {
        this.summaryID = summaryID;
    }

    public boolean createJDSerial() {
        synchronized (SYSerialDao.class) {
            boolean success = false;

            Connection conn = null;
            PreparedStatement pst = null;
            ResultSet rs = null;
            try {
                conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
                DBUtil.setAutoCommit(conn, false);
                pst = conn
                        .prepareStatement("update formmain_1875 f set f.field0006 = (select s.field03 || to_char(sysdate, 'yyyymm') ||   decode(s.field02,    to_char(sysdate, 'yy-mm'),    lpad(s.serial+1, 3, 0),  '001') from fy_zc_serial s   where s.name = 'jdserial'  and s.field01 = (select o.shortname from v3x_org_account o where o.id= ?) ) where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");
                pst.setString(1, accoutID);
                pst.setString(2, String.valueOf(summaryID));

                pst.execute();
                DBUtil.close(pst);

                pst = conn
                        .prepareStatement("update formmain_1875 f set f.field0032 = f.field0006 where f.id = ( select s.form_recordid from col_summary s where s.id = ?)");
                pst.setString(1, String.valueOf(summaryID));

                pst.execute();
                DBUtil.close(pst);

                pst = conn.prepareStatement("update formson_1877 fs set fs.FIELD0023 = (select fm.field0006 from FORMMAIN_1855 fm where fm.id = fs.FORMMAIN_ID) where fs.id = ( select s.form_recordid from col_summary s where s.id = ?)");
                pst.setString(1, String.valueOf(summaryID));
                pst.execute();
                DBUtil.close(pst);

                pst = conn
                        .prepareStatement("update fy_zc_serial f set f.serial = decode(f.field02,                                        to_char(sysdate, 'yy-mm'),                                        f.serial+1,                                        1),f.field02 = to_char(sysdate, 'yy-mm') where f.name = 'jdserial'                         and f.field01 = (select o.shortname                                            from v3x_org_account o                                           where o.id = ?)");

                pst.setString(1, accoutID);
                pst.execute();
                DBUtil.close(pst);

                conn.commit();

            } catch (Exception e) {
                DBUtil.rollback(conn);
                e.printStackTrace();
                log.error("检定校准生成编号发生异常，summaryID为" + summaryID, e);
            } finally {
                DBUtil.setAutoCommit(conn, true);
                DBUtil.close(rs, pst, conn);
            }

            return success;
        }
    }
}
